﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;
namespace _0712518_0712510_LTGame
{
    public class Map : VisibleGameEntity
    {
        public Vector2 StartPosition;
        public int[,] tile;
        public int MaxX;
        public int MaxY;
        public int tile_size;
        public Texture2D texture;
        public Vector2 direction
        {
            get
            {
                Vector2 inputDirection = Vector2.Zero;
                if (Keyboard.GetState().IsKeyDown(Keys.Left))
                    inputDirection.X -= 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Right))
                    inputDirection.X += 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                    inputDirection.Y -= 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                    inputDirection.Y += 1;
                GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
              
                return inputDirection *4;// *speed;
            }
        }
        public Map()
        {
            
        }
        public void LoadMap(string filename)
        {
             List<string> input = new List<string>();
             try
             {

                 FileStream file = new FileStream(filename, FileMode.Open);
                 StreamReader reader = new StreamReader(file, Encoding.UTF8);
                 tile_size = 50;
                 int y = int.Parse(reader.ReadLine());
                 int x = int.Parse(reader.ReadLine());

                   tile = new int[y,x];

                    for (int i = 0; i < y; i++) 
                    {
                        string Line = reader.ReadLine();
                        string[] G = Line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int j = 0; j < x; j++) 
                        {
                            tile[i, j] = int.Parse(G[j]);
                        }
                    }
                    StartPosition.X = 0;
                    StartPosition.Y = 0;
                    MaxX = x * tile_size;
                    MaxY = y * tile_size;

             }
             catch
             {
                 Console.WriteLine("Error");
             }
        }

      
        public void LoadContent(ContentManager Content)
        {
            LoadMap(@"Map\test.txt");
            texture = Content.Load<Texture2D>(@"Map\cell");
        }
        public void centerEntityOnMap(Character zero, int clientWidth, int clientHeight)
        {
            StartPosition.X = zero.position.X - (clientWidth / 2);

            if (StartPosition.X < 0)
            {
                StartPosition.X = 0;
            }

            else if (StartPosition.X + clientWidth >= MaxX)
            {
                StartPosition.X = MaxX - clientWidth;
            }

            StartPosition.Y = zero.position.Y - (clientHeight / 2);

            if (StartPosition.Y < 0)
            {
                StartPosition.Y = 0;
            }

            else if (StartPosition.Y + clientHeight >= MaxY)
            {
                StartPosition.Y = MaxY - clientHeight;
            }
        }
        public void Update(GameTime gameTime, int clientWidth, int clientHeight)
        {
            //StartPosition += direction;
            //if (StartPosition.X < 0)
            //{
            //    StartPosition.X = 0;
            //}
            //if (StartPosition.X + clientWidth >= MaxX)
            //{
            //    StartPosition.X = MaxX - clientWidth;
            //}
            //if (StartPosition.Y < 0)
            //{
            //    StartPosition.Y = 0;
            //}
            //if (StartPosition.Y + clientHeight >= MaxY)
            //{
            //    StartPosition.Y = MaxY - clientHeight;
            //}
           

        }
        public void Draw(SpriteBatch SpriteBatch, int clientWidth, int clientHeight)
        {
            int i,j;
            Vector2 start;
            Vector2 max;
            i = (int)StartPosition.X / tile_size;
            start.X = (StartPosition.X % tile_size) * -1;
            max.X = start.X + clientWidth + (start.X == 0 ? 0 : tile_size);

            j =(int) StartPosition.Y / tile_size; ;
            start.Y = (StartPosition.Y % tile_size) * -1;
            max.Y = start.Y + clientHeight + (start.Y == 0 ? 0 : tile_size);

            for (int y = (int)start.Y; y < max.Y; y += tile_size)
            {
                i = (int)StartPosition.X / tile_size;

                for (int  x = (int)start.X; x < max.X; x += tile_size)
                {

                    if (j >= MaxY/tile_size || i >= MaxX/tile_size)
                    {
                        return;
                    }
                    if (tile[j,i] != 0)
                    {
                        SpriteBatch.Draw(texture, new Rectangle(x, y, tile_size, tile_size), Color.White);
                    }

                   i++;
                }

                j++;
            }

        }
    }
}
